package com.test.controller.admin;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.test.controller.Admininfo.ResData;
import com.test.entity.DepartInfo;
import com.test.entity.FileInfo;
import com.test.service.FileInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.annotation.MultipartConfig;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 文件信息(FileInfo)表控制层
 *
 * @author makejava
 * @since 2022-11-12 11:18:51
 */
@Controller
public class AdminFileInfoController {

    @Autowired
    private FileInfoService fileInfoService;

    //    显示页面
    @GetMapping("admin/file_info")
    public String getDepartInfo() {
        return "admin/file_info/list";
    }

    //    显示页面信息
    @GetMapping("admin/file_info/queryList")
    @ResponseBody
    public String getQueryList(Integer page, FileInfo fileInfo) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(fileInfoService.queryByPage(fileInfo, PageRequest.of(page - 1, 10)));
    }

    //    显示新增页面
    @GetMapping("admin/file_info/add")
    public String getAdd() {
        return "admin/file_info/add_page";
    }

    //    提交新增数据
    @PostMapping("admin/file_info/add_submit")
    @ResponseBody
    public ResData postAddSubmit(FileInfo fileInfo) {
        ResData resData = new ResData();
        int insert = fileInfoService.insert(fileInfo);
        if (insert > 0) {
            resData.setCode(200);
            resData.setMsg("添加成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("添加失败");
        return resData;
    }

    //    显示修改页面
    @GetMapping("admin/file_info/update")
    public ModelAndView getUpdate(Integer id) {
        ModelAndView modelAndView = new ModelAndView("admin/file_info/update_page");
        FileInfo fileInfo = fileInfoService.queryById(id);
        modelAndView.addObject("data", fileInfo);
        return modelAndView;
    }

    //    提交修改信息
    @PostMapping("admin/file_info/update_submit")
    @ResponseBody
    public ResData postUpdateSubmit(Integer id, FileInfo fileInfo) {
        fileInfo.setId(id);
        ResData resData = new ResData();
        int update = fileInfoService.update(fileInfo);
        if (update > 0) {
            resData.setCode(200);
            resData.setMsg("修改成功");
            return resData;
        }
        resData.setCode(200);
        resData.setMsg("修改成功");
        return resData;
    }

    //    删除信息
    @GetMapping("admin/file_info/del")
    @ResponseBody
    public ResData getDelete(Integer id) {
        System.out.println(id);
        ResData resData = new ResData();
        boolean b = fileInfoService.deleteById(id);
        if (b) {
            resData.setCode(1);
            resData.setMsg("删除成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("删除失败");
        return resData;
    }

}

